package com.ias.agdis_r.terminalmanager.restful.base.exception;

import com.ias.agdis_r.terminalmanager.restful.base.common.ErrorCodeConstants;

public class BaseException extends RuntimeException {

    /**
     * 
     */
    private static final long serialVersionUID = 4504203472864850553L;

    protected String errorCode = ErrorCodeConstants.ERR_GENERAL_EXCEPTION;

    public BaseException() {
        super();
    }

    public BaseException(String errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
    }

    public BaseException(String message) {
        super(message);
    }

    public BaseException(String errorCode, String message, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
    }

    public BaseException(Throwable cause) {
        super(cause);
        populateDetail(cause);
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
        populateDetail(cause);
    }

    private void populateDetail(Throwable cause) {
        if (cause instanceof BaseException) {
            BaseException ex = ((BaseException) cause);
            setErrorCode(ex.getErrorCode());
        }
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String toString() {
        String s = getClass().getName();
        String message = getLocalizedMessage();
        if (errorCode != null) {
            if (message == null)
                message = "[" + errorCode + "]";
            else
                message = "[" + errorCode + "] - " + message;
        }
        return (message != null) ? (s + ": " + message) : s;
    }
}
